<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}

if($_POST['sub']!=''){

    $id=empty($_POST['id'])?0:(int)$_POST['id'];
	unset($_POST['id']);
	unset($_POST['sub']);
    
    $oplist=$_POST["oplist"];
    $opval=$_POST["opval"];
    $ouser=trim($_POST["operate_user"]);
    if(empty($ouser)){
        jumpajax('300','主持人用户名不能为空！','');
        
    }
    if(empty($_POST["courtname"])){
        jumpajax('300','法院名称不能为空！','');
        
    }
    if(strpos($ouser,",")>0){
        $ouserlist[]=explode(",",$ouser);
    }else{
        $ouserlist[]=$ouser;
    }
    $ulist=array();
    $uidlist=array();
    while(list($k,$v)=each($ouserlist)){
       $urow=$ezhand->select("member",'id,username','username="'.$v.'"');
        if(!empty($urow)){
            
            $ulist[]=$urow["username"];
            $uidlist[]=$urow["id"];
            
        }       
    }
    $operate_user=implode(",",$ulist);
    $operate_userid=implode(",",$uidlist);
    $zbtype=intval($_POST["zbtype"]);
    $data=array('subject'=>$_POST['subject'],"description"=>$_POST["description"],"operate_user"=>$operate_user,"thumb"=>trim($_POST["attachment_fileName"]),"enabled"=>$_POST["enabled"],
    "userid"=>$_SESSION['EZ_Admin']['id'],    
    "username"=>$_SESSION['EZ_Admin']['name'],
    "operate_userid"=>$operate_userid,
    "fromdate"=>$_POST["fromdate"],
    "todate"=>$_POST["todate"],
    "contect"=>trim($_POST['contect']),
    "tel"=>trim($_POST['tel']),
    "courtname"=>trim($_POST["courtname"]),
    "zbtype"=>$zbtype
    );
    $title=trim($_POST["subject"]);
    if(trim($title)==""){
            jumpajax('300','直播现场标题不能为空！','');
    }      
 	if($id==0){
        $data["addtime"]=time();
	    $id=$ezhand->insert("zhibo_subject",$data);
        $d=array("subjectid"=>$id,"options"=>"主持人");
        $opid=$ezhand->insert("zhibo_option",$d);    
		
	}
	else{
	    $ezhand->update("zhibo_subject",$data,'subjectid="'.$id.'"',1);
	
	}
    //$ezhand->delete('vote_option','subjectid="'.$id.'"');
    if(!empty($oplist)){
        while(list($k,$v)=each($oplist)){
            $v=trim($v);
            if(!empty($v)){
            $d=array("subjectid"=>$id,"option"=>$v);
            if(!empty($opval[$k])){
                $opid=$ezhand->update("zhibo_option",$d,"optionid=".$opval[$k]);
            }else{
                $opid=$ezhand->insert("zhibo_option",$d);    
            }
            
            }
        }
        
    }
    
    jumpajax('200','直播现场保存成功','','closeCurrent');
}
else{
	$id=empty($_GET['id'])?0:(int)$_GET['id'];
	
    if($id==0){
	    $row=array();
	}
	else{
	    $row=$ezhand->select("zhibo_subject",'*','subjectid="'.$id.'"');
        $votedata=$ezhand->select_all("zhibo_option","*","subjectid=".$id." order by optionid");
	}
}
?>